package com.cxl.demo;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class SocketClient {
    public static void main(String[] args) throws Exception {


        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                Socket socket  = null;
                try {
                    socket = new Socket("127.0.0.1",9000);


                OutputStream out  =socket.getOutputStream();
                out.write("发送一个消息".getBytes());
                char[] chars =  new char[1024 * 1024];
                InputStream in  = socket.getInputStream();
                InputStreamReader reader= new InputStreamReader(in);
                int len = reader.read(chars);

                if (len!=-1){
                    String str = new String(chars,0,len);
                    System.out.println("客户端收到信息 : " + str);
                }
                // 关闭
                out.close();
                in.close();
                reader.close();
                socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

}
